package cn.stylefeng.guns.modular.gridsystem.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.date.DateTime;
import cn.hutool.core.util.ObjectUtil;
import cn.stylefeng.guns.core.exception.ServiceException;
import cn.stylefeng.guns.modular.gridsystem.entity.BizGateway;
import cn.stylefeng.guns.modular.gridsystem.entity.CollectionFault;
import cn.stylefeng.guns.modular.gridsystem.enums.CollectionFaultEnum;
import cn.stylefeng.guns.modular.gridsystem.enums.CollectionFaultExceptionEnum;
import cn.stylefeng.guns.modular.gridsystem.mapper.BizGatewayMapper;
import cn.stylefeng.guns.modular.gridsystem.mapper.CollectionFaultMapper;
import cn.stylefeng.guns.modular.gridsystem.model.param.CollectionFaultParam;
import cn.stylefeng.guns.modular.gridsystem.service.CollectionFaultService;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

/**
 * @author zxw 2022/12/17 16:59
 */
@Slf4j
@Service
@RequiredArgsConstructor
public class CollectionFaultServiceImpl extends ServiceImpl<CollectionFaultMapper, CollectionFault> implements CollectionFaultService {

    private final BizGatewayMapper gatewayMapper;

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void reportFault(CollectionFaultParam param) {

        log.info(">>> 采集端正上报故障，设备编码为：{}，故障编码为：{}", param.getGatewayCode(), param.getFaultCode());

        CollectionFault fault = BeanUtil.toBean(param, CollectionFault.class);

        // 处理具体的故障类型
        if (fault.getFaultCode().equals(CollectionFaultEnum.SOUND_CARD_NOT_FOUND.getCode())) {
            fault.setFaultMessage(CollectionFaultEnum.SOUND_CARD_NOT_FOUND.getMessage());
        }

        if (fault.getFaultCode().equals(CollectionFaultEnum.FTP_MODE_FILE_SIZE_ERROR.getCode())) {
            fault.setFaultMessage(CollectionFaultEnum.FTP_MODE_FILE_SIZE_ERROR.getMessage());
        }

        if (fault.getFaultCode().equals(CollectionFaultEnum.NETWORK_CARD_ERROR.getCode())) {
            fault.setFaultMessage(CollectionFaultEnum.NETWORK_CARD_ERROR.getMessage());
        }

        // 设置网关ID
        fault.setGatewayId(queryAndValid(param.getGatewayCode()).getId());
        // 设置上报时间
        fault.setReportTime(DateTime.now());
        // 保存数据
        save(fault);

        log.info(">>> 采集端故障上报成功");

    }

    private BizGateway queryAndValid(String gatewayCode) {

        BizGateway gateway = gatewayMapper.selectOne(new LambdaUpdateWrapper<BizGateway>().eq(BizGateway::getGatewayCode, gatewayCode).last("limit 1"));

        if (ObjectUtil.isEmpty(gateway)) {
            log.warn(">>> 网关不存在,网关编码为：{}", gatewayCode);
            throw new ServiceException(CollectionFaultExceptionEnum.GATEWAY_NOT_EXIST);

        }

        return gateway;

    }

}
